package com.toommi.refuse.controller;

import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.service.VerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * ..
 * 验证码接口
 *
 * @author yangg
 * @since 2019-08-06 09:39
 */
@RestController
@RequestMapping(value = "/api")
@Slf4j
@Api(tags = "验证码")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;
    /**
     * 验证码长度
     */
    @Value("${verify.code.length}")
    private int codeLength;
    /**
     * 验证码有效期
     */
    @Value("${verify.code.expire}")
    private long codeExpire;


    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @param type  验证码类型
     * @return 操作结果
     */
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "验证码类型，1为微信端用户注册，2为APP端用户找回密码", required = true, dataType = "int")
    })
    @PostMapping(value = "/verifyCode", produces = "application/json;charset=UTF-8")
    public CommonResponse verifyCode(@RequestParam String phone, @RequestParam int type) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            String code = verifyCodeService.sendVerifyCode(phone, type, codeLength, codeExpire);
            log.debug("phone number is :{},code is :{}", phone, code);
        } catch (Exception e) {
            log.error("get verify code error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

}
